package me.winds.wrapper.base.view;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.ParameterizedType;

import me.winds.widget.usage.ToastView;
import me.winds.wrapper.base.presenter.BaseMvpPresenter;
import me.winds.wrapper.base.presenter.MvpPresenter;
import me.winds.wrapper.core.NetworkChanged;
import me.winds.wrapper.core.network.ResponseInfo;
import me.winds.wrapper.widget.LoadingDialog;

/**
 * Author:  winds
 * Email:   heardown@163.com
 * Date:    2019/5/26.
 * Desc:
 */
public abstract class WrapperDialogFragment<P extends BaseMvpPresenter> extends DialogFragment implements MvpView {
    protected P presenter;
    protected WrapperDialog progressDialog;
    protected boolean loadSuccess; //数据是否加载成功
    protected boolean loadFinish; //请求是否成功

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter(this.getClass());
        presenter.attachView(this);
        //setStyle(DialogFragment.STYLE_NO_TITLE | DialogFragment.STYLE_NO_FRAME, 0);
        progressDialog = createLoadingDialog(getActivity());
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(getViewLayout(), container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //去除标题栏
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(savedInstanceState);
        loadData(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onStopLoading();
        progressDialog = null;
        presenter.detachView();
        presenter = null;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void beforeSuccess() {
        loadSuccess = true;
        loadFinish = true;
    }

    @Override
    public void onLoading() {
        showLoadingDialog();
        loadSuccess = false;
        loadFinish = false;
    }

    @Override
    public void onStopLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onLoadError(ResponseInfo responseInfo) {
        loadFinish = true;
        loadSuccess = false;
    }

    @Subscribe
    public void networkChanged(NetworkChanged changed) {

    }

    /**
     * @return 布局resourceId
     */
    public abstract int getViewLayout();

    /**
     * 初始化View。或者其他view级第三方控件的初始化,及相关点击事件的绑定
     *
     * @param savedInstanceState
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 获取请求参数，初始化vo对象，并发送请求
     *
     * @param savedInstanceState
     */
    protected abstract void loadData(Bundle savedInstanceState);


    protected P createPresenter(Class clz) {
        ParameterizedType type = (ParameterizedType) clz.getGenericSuperclass();
        Class<? extends MvpPresenter> presenterClazz = (Class<? extends MvpPresenter>) type.getActualTypeArguments()[0];
        try {
            return (P) presenterClazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected WrapperDialog createLoadingDialog(Context context) {
        return new LoadingDialog(context);
    }

    public void showLoadingDialog() {
        if (progressDialog != null && !progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    protected void showToast(String msg) {
        ToastView.showToastInCenter(getActivity(), msg);
    }
}
